package cn.gson.crm.controller.dept;

import cn.gson.crm.controller.BaseServlet;
import cn.gson.crm.model.dao.DeptDao;
import cn.gson.crm.model.dao.EmpDao;
import cn.gson.crm.model.entity.Dept;
import cn.gson.crm.model.entity.Employee;

import java.sql.SQLException;
import java.util.List;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class DeptServlet
 */
@WebServlet("/dept")
public class DeptInsertOrUpdateServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private DeptDao dd = new DeptDao();
	private EmpDao dao = new EmpDao();
	String deptId = null;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void get(HttpServletRequest request, HttpServletResponse response) throws Exception {
		deptId = request.getParameter("id");
		commonMethod(request,response,deptId);
		
		forward("dept/dept_form.jsp");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void post(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id = request.getParameter("deptId");
		String deptName = request.getParameter("deptName");
		String deptManager = request.getParameter("deptManager");
		String updeptId = request.getParameter("updeptId");
		String status = request.getParameter("status");
		
		if(deptName == null || deptName.isEmpty()){
			commonMethod(request,response,deptId);
			errorForward("部门名称不可为空！", "dept/dept_form.jsp");
			return;
		}
		
		
		
		if(updeptId == null || updeptId.isEmpty()){
			commonMethod(request,response,deptId);
			errorForward("请选择上属部门！", "dept/dept_form.jsp");
			return;
		}

		Dept dept1 = dd.searchDeptNameAndSearchStatus(deptName);
		if("创建".equals(status)){
			if(dept1.getDept_status() == 0 && dd.searchDeptName(deptName) > 0){
				dd.deleteDept(Integer.parseInt(dept1.getDept_id()), 1,Integer.parseInt(updeptId),deptManager);
				redirect("dept/form");
				return;
			}
			if(dept1.getDept_status() == 1 && dd.searchDeptName(deptName) > 0){
				commonMethod(request,response,deptId);
				errorForward("该部门已经存在！", "dept/dept_form.jsp");
				return;
			}
		}else{
			if(dept1.getDept_status() == 0){
				dd.deleteDept(Integer.parseInt(dept1.getDept_id()), 1,Integer.parseInt(updeptId),deptManager);
				dd.deleteDept(Integer.parseInt(id), 0,0,"0");
				redirect("dept/form");
				return;
			}
		}
		
		Dept dept = new Dept();
		if(id != null && !id.isEmpty()){
			dept.setDept_id(id);
		}
		dept.setDept_name(deptName);
		dept.setDept_manager(Integer.parseInt(deptManager));
		dept.setUpdapt_id(Integer.parseInt(updeptId));
		
		if(dd.insertDept(dept)){
			redirect("dept/form");
			return;
		}
		commonMethod(request,response,deptId);
		errorForward("保存失败！", "dept/dept_form.jsp");
	}
	
	private void commonMethod(HttpServletRequest request, HttpServletResponse response, String deptId) throws SQLException, Exception {

		List<Dept> deptList = dd.getAllDeptNameAndId();
		request.setAttribute("deptList", deptList);
		//获取所有职务是总管或主管或经理的人
		List<Employee> employee = dao.getAllMananger();
		request.setAttribute("employee", employee);
		
		
		if(deptId != null){
			Dept dept = dd.getOneByDeptId(Integer.parseInt(deptId));
			request.setAttribute("dept", dept);
		}
	}

}
